(function(win, doc, obj) {
  // 定义全局对象
  function Obj(){};
  Obj.prototype = {
    /**
     * 简单cookie获取与设置
     * @param  {string} name  cookie设置或获取
     * @param  {string} value 设置cookie的值
     * @param  {string} op    设置cookie的参数
     */
    cookie: function(name, value, op) {
      // 变量获取
      var op = op || {},
        expires = '',
        d = new Date();
      if (typeof value != 'undefined') {
        if (value === null){ op.expires = -1}; //删除
        if (op.expires && (typeof op.expires == "number" || op.expires.toUTCString)) {
          if (typeof op.expires == "number"){ d.setTime(d.getTime() + (op.expires * 36e5))} // 以小时为单位
          else{ d = op.expires}
          expires = "; expires=" + d.toUTCString();
        }
        doc.cookie = [name, "=", encodeURIComponent(value), expires, op.path ? ("; path=" + op.path) : "", op.domain ? ("; domain=" + op.domain) : "", op.secure ? "; secure" : ""].join('');
      } else{ return decodeURIComponent(doc.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + name + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null}
    },
    topBannerStyle: function(height){
      height = height || 100;
      $('head').append('<style>.topBannerBox{width:100%;z-index:9999;background:#292929;height:'+ height/2 +'px;}.topBannerBox .topBannerBD{position:fixed;top:0;width:100%;height:'+ height/2 +'px;background:rgba(0,0,0,.75);}.topBannerBox .topBannerAnim{-webkit-transform:translateY(0);transform:translateY(0);-webkit-transition:all .5s cubic-bezier(.19,1,.22,1);transition:all .5s cubic-bezier(.19,1,.22,1);z-index:9999;}.topBannerBox .topBannerAnimHidden{-webkit-transform:translateY(-101%);transform:translateY(-101%);-webkit-transition:all .3s cubic-bezier(.55,.055,.675,.19);transition:all .3s cubic-bezier(.55,.055,.675,.19);z-index:9999;}.topBannerBox .close{position:absolute;top:0;right:0;width:24px;height:'+ height/2 +'px;line-height:'+ height/2 +'px;color:#999;padding:0;background:none;font-size:24px;font-family:SimSun,Tahoma,arial,SimHei,sans-serif;text-align:center;text-decoration:none;z-index:9999;}[data-dpr="2"] .topBannerBox,[data-dpr="2"] .topBannerBox. topBannerBD{height:'+ height +'px}[data-dpr="3"] .topBannerBox,[data-dpr="3"] .topBannerBox. topBannerBD{height:'+ height*1.5 +'px}[data-dpr="2"] .topBannerBox .close{width:48px;height:'+ height +'px;line-height:'+ height +'px;}[data-dpr="3"] .topBannerBox .close{width:72px;height:'+ height*1.5 +'px;;line-height:'+ height*1.5 +'px;}.topBannerBox,.topBannerBox *,.topBannerBox:after,.topBannerBox:after,.topBannerBox:before,.topBannerBox:before{box-sizing:border-box}.topBannerBox .txtBox,.topBannerBox .open{position:absolute;top:50%;-webkit-transform:translateY(-50%);transform:translateY(-50%)}.topBannerBox .open{right:32px;display:inline-block;width:88px;line-height:30px;border-radius:3px;font-size:12px;text-decoration:none;color:#fff;text-align:center;white-space:nowrap;background:#ff424b}.topBannerBox .txtBox{left:5px}.topBannerBox .txtBoxTxt{color:#fff;font-size:12px; margin:0 0 0 8px;}.topBannerBox .logoTxt{margin:0 0 2px 0;color:#f9f978;font-size:20px;line-height:1;font-weight:400}.topBannerBox .jisu{color:#a7f96f}.topBannerBox .noad{color:#ffcec5}.topBannerBox .cache{color:#9af5bf}@media screen and (max-width:320px){.topBannerBox .logoTxt{font-size:16px}}@media screen and (max-width:375px){.topBannerBox .logoTxt{font-size:16px}}</style>')
    },
    addBanner: function(content,opt){
      opt = $.extend({height:100,defaltEvent:true},opt);
      this.topBannerStyle(opt.height); //设置样式
      $('body').prepend('<div class="topBannerBox"><div class="topBannerBD topBannerAnim" style="display: block !important;"><a href="javascript:void(0)" class="close">&times;</a>'+content+'</div></div>');
      
      // 添加事件操作
      this.hiddenBanner(opt.expires); //默认设置关闭后失效时间为关闭阅览器
      if(opt.defaltEvent){
        this.topBannerAnim(opt.scrollHeight?opt.scrollHeight:opt.height); //默认执行
        this.topBannerConClick(opt); //默认绑定点击事件
      }
    },
    hiddenBanner: function(expires) {
      var that = this;
      $('body').on('click','.topBannerBox .close', function(event) {
        event.stopPropagation();
        event.preventDefault();
        that.cookie('topBanner2', true, expires?{expires: expires}: {expires: 1}); // 默认过期时间为1小时
        $('.topBannerBox').remove();
      })
    },
    topBannerAnim: function(dis){
      $(win).scroll(function() {
        var obj = $('.topBannerBox .topBannerBD');
        if ($(win).scrollTop() > dis){ obj.attr('class','topBannerBD topBannerAnimHidden')}
        else{ obj.attr('class','topBannerBD topBannerAnim')}
      })
    },
    topBannerConClick: function(opt) {
      var _this = this;
      var ev = opt?opt.event||'click':'click';
      
      // 获取全局的漫画信息
      var data = win.gobal_info||{site_id:'kanman',comic_id:''};
      $('body').on(ev,'.topBannerBox>.topBannerBD',function() {
        _this.schemeFn({
          scheme_Adr:
            'tkanmanapp://goto?page=' +
            (data.comic_id ? 'dir' : 'index') +
            '&comic_id=' +
            data.comic_id,
          timeout: 600,
          iyouman: { site: '爱优漫', ckey: 'CK1383325844989' },
          shenmanhua: { site: '神漫画', ckey: 'CK1383322645544' },
          manhuatai: { site: '漫画台', ckey: 'CK138297596322' },
          kanman: { site: '看漫网', ckey: 'CK1382108356199' },
          douluodalu3: { site: '漫画台', ckey: 'CK138297596322' },
          isaman: { site: '爱飒漫画', ckey: 'CK1383328534446' },
          zymk: { site: '知音漫客', ckey: 'CK1353950509167' }
        })
      })
    },
    schemeFn: function(data) {
      var host = 'kanman' || location.host.replace(/([a-z]{1,6}\.)?(.+)(\.[a-z]{2,4})$/,'$2');
      var url = 'http://a.app.qq.com/o/simple.jsp?pkgname=com.wbxm.icartoon&ckey=' + data[host].ckey;
      var UA = navigator.userAgent.toLowerCase();
      if(UA.match(/(iPhone|iPod|iPad);?/i)){
        //抛出schemes,打开app对应页面
        win.location.href = data.scheme_Adr;
        //由于部分ios中打开app后，WAP页面会被挂起，定时器不会被执行。因此可以做下优化：
        //WAP页重新被聚焦后，如果超过1s，认为APP被打开了，重新聚焦时就不必再跳转到APP下载页
        var startTime = Date.now();
        var delayTime = /safari/.test(UA)?3000:1000;
        var timer = setInterval(function () {
          var endTime = Date.now();
          if (endTime - startTime > delayTime) {
            clearInterval(timer);
            win.location.href = url;
          }
        }, 25);
      }else{
        // 如果用户已经安装了应用 抛出schemes,打开app对应页面
        var ifr = doc.createElement('iframe');
        ifr.style.display = 'none';
        doc.body.appendChild(ifr);
        ifr.src = data.scheme_Adr;
        //跳转下载页
        setTimeout(function () {
          win.location.href = url;
        }, data.timeout);
      }
    },
    init: function(data) {
      if(!topBanner.cookie('topBanner2')){
        topBanner.addBanner('<div class="txtBox"><h2 class="logoTxt">《' +data.site+ '》APP</h2><p class="txtBoxTxt"><span class="jisu">极速</span> <span class="noad">无广告</span> <span class="cache">离线缓存</span></p></div> <a href="javascript:;" class="open">打开APP阅读</a>',{scrollHeight:300});
      }
    }
  };
  win[obj] = new Obj(); // 暴露的全局对象名称
})(window,document,'topBanner');
